/**
 * Title:        流程计划扫描时间检测
 * Company:      泛微软件
 */


package checkDetail;

import general.BaseBean;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;

import weaver.general.TimeUtil;
import weaver.general.Util;
import weaver.system.ThreadWorkTimer;
import weaver.workflow.workflow.SysWorkFlowAlert;



public class WorkflowPlanCheck extends BaseBean implements CheckBase  {

    @Override
    public JSONObject checkData(HashMap dataMap) {
        setRecordSet();
        (new BaseBean()).writeLog("WorkflowPlanCheck  start");

        List resultList = new ArrayList();
        String result = "";
        recordSet.executeProc("SystemSet_Select","");
        recordSet.next();
        String scan = Util.null2String(recordSet.getString("scan"));
        if(!"".equals(scan)) {
            if(getIntValue(scan) <= 0){
                result = resultNeedFix;
                Map resultMap = new HashMap<String,String>();
                resultMap.put("showMessage","流程计划扫描频率非数字。");
                resultMap.put("linkurl","/workflow/WorkflowSettings.jsp");

                resultMap.put("inputnamelist","scantime");
                resultMap.put("buttonname","修复");
                resultList.add(resultMap);
            }else if(getthreadTime() != getIntValue(scan)){
                result = resultNeedFix;
                resultList.add("流程计划扫描频率(" + scan + "分钟)和线程中(" + getthreadTime() + "分钟)不一致，请重启服务。");
            }else{
                result = resultOk;
                resultList.add("流程计划扫描时间，测试正常!");
            }
       }
        (new BaseBean()).writeLog("WorkflowPlanCheck  end");
        return formatReturnJsonCheck(result, resultList,this);
    }

    @Override
    public JSONObject fixData(HashMap dataMap) {

        setRecordSet();
        (new BaseBean()).writeLog("WorkflowPlanCheck  start");

        if(dataMap!= null && dataMap.get("scantime") != null ){
            List resultList = new ArrayList();
            String result = resultOk;
            String divid = (String)dataMap.get("divid");
            Map resultMap = new HashMap<String,String>();
            resultMap.put("divid",divid);
            int scantime = Util.getIntValue((String)dataMap.get("scantime"));
            if(scantime <= 0){
                result = resultNeedFix;
                resultMap.put("result",resultNeedFix);
                resultMap.put("fixMessage","流程计划扫描时间修复失败");
                resultList.add(resultMap);
                return formatReturnJsonCheck(result, resultList,this);
            }else{
                recordSet.executeUpdate(" update SystemSet set scan = " + scantime + " ");
            }
        }
        needReload = "1";
        return checkData(dataMap);
    }

    @Override
    public String getClassName() {
        return "流程计划扫描时间检测";
    }

    private long getthreadTime(){

        ThreadGroup group = Thread.currentThread().getThreadGroup();
        ThreadGroup topGroup = group;
        // 遍历线程组树，获取根线程组  
        while (group != null) {
            topGroup = group;
            group = group.getParent();
        }
        // 激活的线程数加倍  
        int estimatedSize = topGroup.activeCount() * 2;
        Thread[] slackList = new Thread[estimatedSize];
        // 获取根线程组的所有线程  
        int actualSize = topGroup.enumerate(slackList);
        // copy into a list that is the exact size  
        Thread[] list = new Thread[actualSize];
        System.arraycopy(slackList, 0, list, 0, actualSize);

        long timelimit = 0l;
        try{
            for (Thread thread : list) {
                Object tid = thread.toString();
                Class<?> rthreadClazz = thread.getClass();
                String rthreadDesc = "";
                Class<?> clazz = thread.getClass();
                if (clazz.equals(ThreadWorkTimer.class)) {
                    ThreadWorkTimer twt = (ThreadWorkTimer) thread;
                    Field field = clazz.getDeclaredField("work");
                    field.setAccessible(true);
                    Object obj = field.get(twt);
                    
                    Field timefield = clazz.getDeclaredField("m_timer");
                    timefield.setAccessible(true);
                    if (rthreadClazz.equals(SysWorkFlowAlert.class)) {
                        timelimit = (Long)timefield.get(twt);
                        break;
                    }
                }
            }
        }catch(Exception e){
            
        }
        return timelimit/1000/60;
    }

    @Override
    public String getClassDesc() {
        return "检测扫描时间是否设置，进程中的扫描时间是否和设置的一样";
    }

}



